<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="用工日期" prop="workTime">
        <el-date-picker
          v-model="queryParams.workTime"
          size="small"
          style="width: 200px"
          value-format="yyyy-MM-dd"
          type="date"
          range-separator="-"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            v-hasPermi="['job:employmentPlan:import']"
          >导入计划名单</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['job:employmentPlan:export']"
          >导出计划名单模板</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="handleSet"
            v-hasPermi="['job:employmentPlan:ageSet']"
          >用工年龄设置</el-button>
        </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="年龄区间" prop="startAge">
              <el-input v-model="form.startAge" placeholder="" maxlength="30" >
                <i slot="suffix" style="margin-right: 10px">岁</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="至" prop="endAge">
              <el-input v-model="form.endAge"  placeholder=""  maxlength="32" >
                <i slot="suffix" style="margin-right: 10px">岁</i>
              </el-input>

            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

     <el-table v-loading="loading" :data="employmentPlanList" :row-class-name="tableRowClassName"> >
       <el-table-column label="用工需求单" prop="orderCode"  min-width="100"/>
      <el-table-column label="用工日期" prop="workTime" :formatter="dateFormatter" width="200"/>
      <el-table-column label="需求人数" prop="totalNum" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="报名人数" prop="signupNum" :show-overflow-tooltip="true" width="200"/>

       <el-table-column label="操作" align="center"    class-name="small-padding fixed-width">
         <template slot-scope="scope">
           <el-button
             size="mini"
             type="text"
             icon="el-icon-view"
             @click="handleView(scope.row)"
             v-hasPermi="['job:employmentPlan:detail']"
           >详情</el-button>

         </template>
       </el-table-column>
    </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件</span>
        </div>

      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {listEmploymentPlanStatistics,addAgeRange,modifyAgeRange,getAgeRange} from "@/api/job/employmentPlan/employmentPlanApi";
import { getToken } from "@/utils/auth";
export default {
  name: "employmentPlan",
  created() {
    this.getList();

  },

  data() {
    return {

      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "工具类导入",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/employment/plan/addEmploymentPlan"
      },
      dialogVisible:false,
      signUpLoading: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      employmentPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      signUp: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //渠道名称
        groupName: undefined,
        //联系人
        workerName: undefined,
      },

      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        startAge: [
          { required: true, message: "开始年龄不能为空", trigger: "blur" },
          { pattern:/^[0-9]*$/, message: '请输正整数' , trigger: "blur"}



        ],
        endAge: [
          { required: true, message: "结束不能为空", trigger: "blur" },
          { pattern:/^[0-9]*$/, message: '请输正整数' , trigger: "blur"}

        ],

      },
    };
  },


      methods: {
        /**
         * 异常标记颜色
         * @param row
         * @param rowIndex
         * @returns {string}
         */
        tableRowClassName({row, rowIndex}) {
          if (row.status=== 0) {
            return 'warning-row';
          }
          return '';
        },
        /** 获取用工统计列表 */
        getList() {
          this.loading = true;
          listEmploymentPlanStatistics(this.addDateRange(this.queryParams, this.dateRange)).then(
            response => {
              this.employmentPlanList = response.rows;
              this.total = response.total;
              this.loading = false;
            }
          );
        },
        dateFormatter (row, column) {
          let datetime = row.workTime;
          if(datetime){
            datetime = new Date(datetime);
            let y = datetime.getFullYear() + '-';
            let mon = datetime.getMonth()+1 + '-';
            let d = datetime.getDate();
            return y + mon + d;
          }
          return ''
        },
        // 取消按钮
        cancel() {
          this.open = false;
          this.reset();
        },

        /** 搜索按钮操作 */
        handleQuery() {
          this.queryParams.pageNum = 1;
          this.getList();
        },

        /** 重置按钮操作 */
        resetQuery() {
          this.dateRange = [];
          this.resetForm("queryForm");
          this.handleQuery();
        },
       // 表单重置
        reset() {
          this.form = {
            id:undefined,
            startAge: undefined,
            endAge: undefined

          };
          this.resetForm("form");
        },
        /**
         * 设置年龄区间
         */
        handleSet( ) {
          this.reset();
          getAgeRange().then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改";
          });
        },

        handleView(row) {
          var datetime =row.workTime;
          datetime = new Date(datetime);
          let y = datetime.getFullYear() + '-';
          let mon = datetime.getMonth()+1 + '-';
          let d = datetime.getDate();
         var workTime=  y + mon + d;
          this.$router.push({path:"/staffMember/employmentPlanDetail",query:{workTime:workTime,orderCode:row.orderCode}});
        },

        /** 下载模板操作 */
        handleExport() {
          var fileName ="jyygmd.xlsx";
          window.location.href = process.env.VUE_APP_BASE_API + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + false;
        },

        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
          this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
          this.upload.open = false;
          this.upload.isUploading = false;
          this.$refs.upload.clearFiles();
          this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
          this.getList();
        },
        // 提交上传文件
        submitFileForm() {
          this.$refs.upload.submit();
        },
        // 提交上传文件
        submitImgForm() {
          this.$refs.upload.submit();
        },
        /** 导入按钮操作 */
        handleImport() {
          this.upload.title = "导入计划名单";
          this.upload.open = true;
        },

        /** 提交按钮 */
        submitForm: function() {
          this.$refs["form"].validate(valid => {
            if (valid) {
              if (this.form.id != undefined) {
                modifyAgeRange(this.form).then(response => {
                  this.msgSuccess("设置成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                addAgeRange(this.form).then(response => {
                  this.msgSuccess("设置成功");
                  this.open = false;
                  this.getList();
                });
              }
            }
          });
        },

      }
};
</script>
